import { defineStore } from "pinia";
import getters from '@/store/getters'
const viewsComponent: Record<string, any> = import.meta.glob("../../c-views/**/*.vue");
import { RouteRecordRaw } from 'vue-router'
export const useDynamicRouting = defineStore({
  id: 'dynamicRouting',
  persist: {
    enabled: true,//开启数据持久化
    strategies: [
      {
        key: 'dynamicRouting',//给一个要保存的名称
        storage: sessionStorage,//sessionStorage / localStorage 存储方式
      }
    ]
  },
  state: () => {
    return {
      routes: [] as Array<RouteRecordRaw>
    }
  },
  actions: {
    addRouteAll(menu: any[], router: any) {  //RoleMenu就是接口返回的数据的类型

      menu.forEach(item => {
        if (viewsComponent['../../c-views/' + item.component + '.vue']) {
          this.addRouteItem(item, router)
        }
        if (item.children && item.children.length > 0) {
          this.addRouteAll(item.children, router)
        }
      })
    },
    addRouteItem(route: any, router: any) {

      const path = route.routePath
      const routeBaseInfo: RouteRecordRaw = {
        path,
        name: route.name,
        component: viewsComponent['../../c-views/' + route.component + '.vue'],
        redirect: route.redirect,
        meta: {
          parentPath: route.meta
        },
      }
      console.log(viewsComponent, 33323232322332);

      router.addRoute(routeBaseInfo)
    }
  }
});
